package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IStudentService;
import com.mdd.admin.validate.student.StudentCreateValidate;
import com.mdd.admin.validate.student.StudentSearchValidate;
import com.mdd.admin.validate.student.StudentUpdateValidate;
import com.mdd.admin.vo.student.StudentDetailVo;
import com.mdd.admin.vo.student.StudentListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.student.Student;
import com.mdd.common.enums.ErrorEnum;
import com.mdd.common.exception.LoginException;
import com.mdd.common.exception.OperateException;
import com.mdd.common.exception.TestException;
import com.mdd.common.mapper.student.StudentMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import com.mdd.admin.validate.student.StudentCreateValidate;
import com.mdd.admin.validate.student.StudentUpdateValidate;
import com.mdd.admin.validate.student.StudentSearchValidate;
import java.util.*;

/**
 * 学生实现类
 * @author LikeAdmin
 */
@Service
public class StudentServiceImpl implements IStudentService {
        
    @Resource
    StudentMapper studentMapper;

    /**
     * 学生列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<StudentListedVo>
     */
    @Override
    public PageResult<StudentListedVo> list(PageValidate pageValidate, StudentSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        studentMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:name:str",
            "=:age:int",
            "=:sex:int",
        });

        IPage<Student> iPage = studentMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<StudentListedVo> list = new LinkedList<>();
        for(Student item : iPage.getRecords()) {
            StudentListedVo vo = new StudentListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 学生详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return Student
     */
    @Override
    public StudentDetailVo detail(Integer id) {
        Student model = studentMapper.selectOne(
                new QueryWrapper<Student>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        StudentDetailVo vo = new StudentDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 学生新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(StudentCreateValidate createValidate) {
//        Student model = new Student();
//        model.setName(createValidate.getName());
//        model.setAge(createValidate.getAge());
//        model.setSex(createValidate.getSex());
//        model.setCreateTime(System.currentTimeMillis() / 1000);
//        model.setUpdateTime(System.currentTimeMillis() / 1000);
//        studentMapper.insert(model);
        throw new OperateException("888");
    }

    /**
     * 学生编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(StudentUpdateValidate updateValidate) {
        Student model = studentMapper.selectOne(
                new QueryWrapper<Student>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setAge(updateValidate.getAge());
        model.setSex(updateValidate.getSex());
        model.setUpdateTime(System.currentTimeMillis() / 1000);
        studentMapper.updateById(model);
    }

    /**
     * 学生删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Student model = studentMapper.selectOne(
                new QueryWrapper<Student>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(System.currentTimeMillis() / 1000);
        studentMapper.updateById(model);
    }

}
